#ifndef __TIMER_H__
#define __TIMER_H__
#include "Timestamp.h"
#include "NonCopyable.h"

#include <atomic>
#include <functional>

using muduo::Timestamp;

class Timer : NonCopyable
{
public:
    typedef std::function<void()> TimerCallback;
    Timer(TimerCallback cb, Timestamp when, double interval)
    : _callback(std::move(cb))
    , _expiration(when)
    , _interval(interval)
    , _repeat(interval > 0.0)
    , _sequence(++_s_numCreated)
    {}

    void run() const
    {
        _callback();
    }

    Timestamp expiration() const { return _expiration; }
    bool repeat() const { return _repeat; }
    int64_t sequence() const { return _sequence; }

    void restart(Timestamp now);
    static int64_t numCreated() { return _s_numCreated.load(); }

private:
    const TimerCallback _callback;
    Timestamp _expiration;
    const double _interval;
    const bool _repeat;
    const int64_t _sequence;

    static std::atomic_int64_t _s_numCreated;
};

#endif